import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.io.*;
import java.net.URL;

public class JScrollPaneTest extends JFrame {
    /**
     * 构造一个有文本的frame
     * @param help_panel 帮助面板
     */
    public  JScrollPaneTest(JPanel help_panel){
        this.setLayout(null);

        String text = null;
        InputStream is = this.getClass().getResourceAsStream("help.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder stringBuilder = new StringBuilder();
        String temp = "";
        try {
            while ((temp = br.readLine()) != null) {
                // 拼接换行符
                stringBuilder.append(temp + "\n");
            }
            br.close();
            text = stringBuilder.toString();
        }catch(IOException ei){
            ei.printStackTrace();
        }
        StyledDocument doc = new DefaultStyledDocument();
        try {
            doc.insertString(doc.getLength(), text, null);
        }catch(BadLocationException ex){
            ex.printStackTrace();
        }
        JTextPane textPane = new JTextPane(doc);
        textPane.setEditable(false);
        JScrollPane scrollPane=new JScrollPane(textPane);
        scrollPane.setBounds(150,200,700,320);


        this.getContentPane().add(scrollPane);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setBounds(0,0,1000,600);
        this.add(help_panel);
    }
}
